package com.kyzh.core.activities

import android.os.Bundle
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Game
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.kyzh.core.adapters.v3.GameListBorderAdapter
import kotlinx.android.synthetic.main.activity_other_sort.*

class OtherSortActivity : BaseActivity() {
    private var type = "1"
    private val beans = arrayListOf<Game>()
    private val adapter = GameListBorderAdapter(R.layout.item_game_border,beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_other_sort)
        type = intent.getStringExtra("type")?:"1"
        titleView.setText(intent.getStringExtra("name")?:getString(R.string.defaultCategory))
        rvList.adapter = adapter
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
    }




    fun getData(){
        GameRequest.otherSort(type,p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        GameRequest.otherSort(type,1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}